package cn.qqtheme.picker;

import android.content.Context;
import androidx.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;

import cn.qqtheme.framework.widget.WheelView;

/**
 * 双列选择器
 *
 * @author yqs
 */
public class DoubleOptionPicker extends cn.qqtheme.framework.picker.WheelPicker {
    protected ArrayList<String> firstColumnDatas = new ArrayList<String>();
    protected ArrayList<String> secondColumnDatas = new ArrayList<String>();

    private OnOptionPickListener onOptionPickListener;
    private int firstSelectedOption = 0;
    private int secondSelectedOption = 0;

    private String label1 = "";
    private String label2 = "";

    /**
     * Instantiates a new Option picker.
     *
     * @param context    the context
     * @param firstDatas the options
     */
    public DoubleOptionPicker(Context context, String[] firstDatas, String[] secondDatas) {
        super(context);
        this.firstColumnDatas.addAll(Arrays.asList(firstDatas));
        this.secondColumnDatas.addAll(Arrays.asList(secondDatas));
    }

    public DoubleOptionPicker(Context context, ArrayList<String> firstColumnDatas, ArrayList<String> secondColumnDatas) {
        super(context);
        this.firstColumnDatas.addAll(firstColumnDatas);
        this.secondColumnDatas.addAll(secondColumnDatas);
    }

    /**
     * 设置显示的单位，如身高为cm、体重为kg
     */
    public void setLabel1(String label1) {
        this.label1 = label1;
    }

    public void setLabel2(String label2) {
        this.label2 = label2;
    }

    /**
     * 设置默认选中的项的索引
     */
    public void setFirstSelectedIndex(int index) {
        if (index >= 0 && index < firstColumnDatas.size()) {
            firstSelectedOption = index;
        }
    }

    public void setSecondSelectedIndex(int index) {
        if (index >= 0 && index < secondColumnDatas.size()) {
            secondSelectedOption = index;
        }
    }

    /**
     * 设置默认选中的项
     */
    public void setFirstSelectedItem(String option) {
        setFirstSelectedIndex(firstColumnDatas.indexOf(option));
    }

    /**
     * 设置默认选中的项
     */
    public void setSecondSelectedItem(String option) {
        setSecondSelectedIndex(secondColumnDatas.indexOf(option));
    }

    public void setOnOptionPickListener(OnOptionPickListener listener) {
        this.onOptionPickListener = listener;
    }

    @Override
    @NonNull
    protected View makeCenterView() {
        if (firstColumnDatas.size() == 0) {
            throw new IllegalArgumentException("please initial options at first, can't be empty");
        }
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setGravity(Gravity.CENTER);
        WheelView optionView = new WheelView(context);
        optionView.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
        optionView.setTextSize(textSize);
        optionView.setTextColor(textColorNormal, textColorFocus);
        optionView.setLineVisible(lineVisible);
        optionView.setLineColor(lineColor);
        optionView.setOffset(offset);
        layout.addView(optionView);
        TextView labelView1 = new TextView(context);
        labelView1.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
        labelView1.setTextColor(textColorFocus);
        labelView1.setTextSize(textSize);
        layout.addView(labelView1);
        if (!TextUtils.isEmpty(label1)) {
            labelView1.setText(label1);
        }
        WheelView secondOptionView = new WheelView(context);
        secondOptionView.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
        secondOptionView.setTextSize(textSize);
        secondOptionView.setTextColor(textColorNormal, textColorFocus);
        secondOptionView.setLineVisible(lineVisible);
        secondOptionView.setLineColor(lineColor);
        secondOptionView.setOffset(offset);
        layout.addView(secondOptionView);

        TextView labelView2 = new TextView(context);
        labelView2.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
        labelView2.setTextColor(textColorFocus);
        labelView2.setTextSize(textSize);
        layout.addView(labelView2);
        if (!TextUtils.isEmpty(label2)) {
            labelView2.setText(label2);
        }
        optionView.setItems(firstColumnDatas, firstSelectedOption);
        optionView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                firstSelectedOption = selectedIndex;
            }
        });

        secondOptionView.setItems(secondColumnDatas, secondSelectedOption);
        secondOptionView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                secondSelectedOption = selectedIndex;
            }
        });
        return layout;
    }

    @Override
    public void onSubmit() {
        if (onOptionPickListener != null) {
            onOptionPickListener.onOptionPicked(firstSelectedOption, secondSelectedOption, firstColumnDatas.get(firstSelectedOption)
                    , secondColumnDatas.get(secondSelectedOption));
        }
    }

    /**
     * Gets selected option.
     *
     * @return the selected option
     */
    public String getFirstSelectedOption() {
        return firstColumnDatas.get(firstSelectedOption);
    }

    public String getSecondSelectedOption() {
        return secondColumnDatas.get(secondSelectedOption);
    }

    /**
     * Get the selected position.
     *
     * @return the selected position
     */
    public int getSelectedPosition() {
        return firstSelectedOption;
    }

    /**
     * The interface On option pick listener.
     */
    public interface OnOptionPickListener {

        void onOptionPicked(int firstPosition, int secondPosition, String firstSelected, String secondSelected);

    }

}
